Method: rich.pretty.traverse.<locals>._traverse
Calls: 452, Exceptions: 1, Paths: 25Back
Path 1: 272 calls (0.6)
1 (76) 2 (27) 0 (15) None (14) 'Hello World' (11) 'foo' (9) 3 (9) 'bar' (8) Console (7) 3.1427 (6)
False (196) True (76)
1 (79) 0 (76) 3 (51) 2 (46) 4 (14) 5 (6)
Node (272)
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
Path 2: 61 calls (0.13)
list (19) ('Paul Atreides', 'Vladimir Harkonnen', 'Thufir Hawat') (6) (False, True, None) (6) (1,) (6) [1, 2, 3] (5) ['foo', 'bar', 'baz'] (3) tuple (...
False (45) True (16)
1 (27) 0 (16) 2 (10) 3 (6) 4 (2)
Node (61)
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
Path 3: 30 calls (0.07)
dict (28) defaultdict (1) test_user_dict.
True (17) False (13)
0 (17) 1 (6) 2 (4) 3 (3)
Node (30)
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
Path 4: 23 calls (0.05)
Layout (14) test_reference_cycle_custom_repr.
True (19) False (4)
0 (19) 1 (2) 2 (2)
Node (23)
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
Path 5: 11 calls (0.02)
test_reference_cycle_dataclass.
True (7) False (4)
0 (7) 1 (2) 2 (2)
Node (11)
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
Path 6: 10 calls (0.02)
list (2) test_reference_cycle_dataclass.
False (10)
1 (5) 2 (4) 4 (1)
Node (10)
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
Path 7: 9 calls (0.02)
test_reference_cycle_attrs. True (5) False (4) 0 (5) 1 (2) 2 (2) Node (9) Path 8: 7 calls (0.02) dict (4) {'bar': True} (1) [1, 2, 3] (1) ['a', 'b', 'c'] (1) False (6) True (1) 1 (2) 3 (2) 0 (1) 2 (1) 4 (1) Node (7) Path 9: 7 calls (0.02) test_reference_cycle_namedtuple. True (4) False (3) 0 (4) 2 (3) Node (7) Path 10: 5 calls (0.01) Layout (4) test_tuple_rich_repr_default. True (5) 0 (5) Node (5) Path 11: 2 calls (0.0) {} (1) test_pretty_namedtuple_fields_invalid_type. False (1) True (1) 2 (1) 0 (1) Node (2) Path 12: 2 calls (0.0) test_pretty_namedtuple_custom_repr. True (2) 0 (2) Node (2) Path 13: 1 calls (0.0) Thing (1) True (1) 0 (1) Node (1) Path 14: 1 calls (0.0) StockKeepingUnit (1) False (1) 1 (1) Node (1) Path 15: 1 calls (0.0) test_broken_getattr. True (1) 0 (1) Node (1) ZeroDivisionError (1) Path 16: 1 calls (0.0) test_max_depth_rich_repr. False (1) 2 (1) Node (1) Path 17: 1 calls (0.0) test_max_depth_attrs. False (1) 2 (1) Node (1) Path 18: 1 calls (0.0) test_max_depth_dataclass. False (1) 2 (1) Node (1) Path 19: 1 calls (0.0) [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] (1) True (1) 0 (1) Node (1) Path 20: 1 calls (0.0) {'foo': 1, 'bar': 2, 'egg': 3} (1) True (1) 0 (1) Node (1) Path 21: 1 calls (0.0) test_attrs. True (1) 0 (1) Node (1) Path 22: 1 calls (0.0) test_attrs_empty. True (1) 0 (1) Node (1) Path 23: 1 calls (0.0) test_lying_attribute. True (1) 0 (1) Node (1) Path 24: 1 calls (0.0) Foo (1) True (1) 0 (1) Node (1) Path 25: 1 calls (0.0) Bar (1) True (1) 0 (1) Node (1)
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node
1def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
2 """Walk the object depth first."""
3
4 obj_id = id(obj)
5 if obj_id in visited_ids:
6 # Recursion detected
7 return Node(value_repr="...")
8
9 obj_type = type(obj)
10 py_version = (sys.version_info.major, sys.version_info.minor)
11 children: List[Node]
12 reached_max_depth = max_depth is not None and depth >= max_depth
13
14 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
15 for arg in rich_args:
16 if _safe_isinstance(arg, tuple):
17 if len(arg) == 3:
18 key, child, default = arg
19 if default == child:
20 continue
21 yield key, child
22 elif len(arg) == 2:
23 key, child = arg
24 yield key, child
25 elif len(arg) == 1:
26 yield arg[0]
27 else:
28 yield arg
29
30 try:
31 fake_attributes = hasattr(
32 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
33 )
34 except Exception:
35 fake_attributes = False
36
37 rich_repr_result: Optional[RichReprResult] = None
38 if not fake_attributes:
39 try:
40 if hasattr(obj, "__rich_repr__") and not isclass(obj):
41 rich_repr_result = obj.__rich_repr__()
42 except Exception:
43 pass
44
45 if rich_repr_result is not None:
46 push_visited(obj_id)
47 angular = getattr(obj.__rich_repr__, "angular", False)
48 args = list(iter_rich_args(rich_repr_result))
49 class_name = obj.__class__.__name__
50
51 if args:
52 children = []
53 append = children.append
54
55 if reached_max_depth:
56 if angular:
57 node = Node(value_repr=f"<{class_name}...>")
58 else:
59 node = Node(value_repr=f"{class_name}(...)")
60 else:
61 if angular:
62 node = Node(
63 open_brace=f"<{class_name} ",
64 close_brace=">",
65 children=children,
66 last=root,
67 separator=" ",
68 )
69 else:
70 node = Node(
71 open_brace=f"{class_name}(",
72 close_brace=")",
73 children=children,
74 last=root,
75 )
76 for last, arg in loop_last(args):
77 if _safe_isinstance(arg, tuple):
78 key, child = arg
79 child_node = _traverse(child, depth=depth + 1)
80 child_node.last = last
81 child_node.key_repr = key
82 child_node.key_separator = "="
83 append(child_node)
84 else:
85 child_node = _traverse(arg, depth=depth + 1)
86 child_node.last = last
87 append(child_node)
88 else:
89 node = Node(
90 value_repr=f"<{class_name}>" if angular else f"{class_name}()",
91 children=[],
92 last=root,
93 )
94 pop_visited(obj_id)
95 elif _is_attr_object(obj) and not fake_attributes:
96 push_visited(obj_id)
97 children = []
98 append = children.append
99
100 attr_fields = _get_attr_fields(obj)
101 if attr_fields:
102 if reached_max_depth:
103 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
104 else:
105 node = Node(
106 open_brace=f"{obj.__class__.__name__}(",
107 close_brace=")",
108 children=children,
109 last=root,
110 )
111
112 def iter_attrs() -> Iterable[
113 Tuple[str, Any, Optional[Callable[[Any], str]]]
114 ]:
115 """Iterate over attr fields and values."""
116 for attr in attr_fields:
117 if attr.repr:
118 try:
119 value = getattr(obj, attr.name)
120 except Exception as error:
121 # Can happen, albeit rarely
122 yield (attr.name, error, None)
123 else:
124 yield (
125 attr.name,
126 value,
127 attr.repr if callable(attr.repr) else None,
128 )
129
130 for last, (name, value, repr_callable) in loop_last(iter_attrs()):
131 if repr_callable:
132 child_node = Node(value_repr=str(repr_callable(value)))
133 else:
134 child_node = _traverse(value, depth=depth + 1)
135 child_node.last = last
136 child_node.key_repr = name
137 child_node.key_separator = "="
138 append(child_node)
139 else:
140 node = Node(
141 value_repr=f"{obj.__class__.__name__}()", children=[], last=root
142 )
143 pop_visited(obj_id)
144 elif (
145 is_dataclass(obj)
146 and not _safe_isinstance(obj, type)
147 and not fake_attributes
148 and (_is_dataclass_repr(obj) or py_version == (3, 6))
149 ):
150 push_visited(obj_id)
151 children = []
152 append = children.append
153 if reached_max_depth:
154 node = Node(value_repr=f"{obj.__class__.__name__}(...)")
155 else:
156 node = Node(
157 open_brace=f"{obj.__class__.__name__}(",
158 close_brace=")",
159 children=children,
160 last=root,
161 )
162
163 for last, field in loop_last(
164 field for field in fields(obj) if field.repr
165 ):
166 child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
167 child_node.key_repr = field.name
168 child_node.last = last
169 child_node.key_separator = "="
170 append(child_node)
171
172 pop_visited(obj_id)
173 elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
174 push_visited(obj_id)
175 class_name = obj.__class__.__name__
176 if reached_max_depth:
177 # If we've reached the max depth, we still show the class name, but not its contents
178 node = Node(
179 value_repr=f"{class_name}(...)",
180 )
181 else:
182 children = []
183 append = children.append
184 node = Node(
185 open_brace=f"{class_name}(",
186 close_brace=")",
187 children=children,
188 empty=f"{class_name}()",
189 )
190 for last, (key, value) in loop_last(obj._asdict().items()):
191 child_node = _traverse(value, depth=depth + 1)
192 child_node.key_repr = key
193 child_node.last = last
194 child_node.key_separator = "="
195 append(child_node)
196 pop_visited(obj_id)
197 elif _safe_isinstance(obj, _CONTAINERS):
198 for container_type in _CONTAINERS:
199 if _safe_isinstance(obj, container_type):
200 obj_type = container_type
201 break
202
203 push_visited(obj_id)
204
205 open_brace, close_brace, empty = _BRACES[obj_type](obj)
206
207 if reached_max_depth:
208 node = Node(value_repr=f"{open_brace}...{close_brace}")
209 elif obj_type.__repr__ != type(obj).__repr__:
210 node = Node(value_repr=to_repr(obj), last=root)
211 elif obj:
212 children = []
213 node = Node(
214 open_brace=open_brace,
215 close_brace=close_brace,
216 children=children,
217 last=root,
218 )
219 append = children.append
220 num_items = len(obj)
221 last_item_index = num_items - 1
222
223 if _safe_isinstance(obj, _MAPPING_CONTAINERS):
224 iter_items = iter(obj.items())
225 if max_length is not None:
226 iter_items = islice(iter_items, max_length)
227 for index, (key, child) in enumerate(iter_items):
228 child_node = _traverse(child, depth=depth + 1)
229 child_node.key_repr = to_repr(key)
230 child_node.last = index == last_item_index
231 append(child_node)
232 else:
233 iter_values = iter(obj)
234 if max_length is not None:
235 iter_values = islice(iter_values, max_length)
236 for index, child in enumerate(iter_values):
237 child_node = _traverse(child, depth=depth + 1)
238 child_node.last = index == last_item_index
239 append(child_node)
240 if max_length is not None and num_items > max_length:
241 append(Node(value_repr=f"... +{num_items - max_length}", last=True))
242 else:
243 node = Node(empty=empty, children=[], last=root)
244
245 pop_visited(obj_id)
246 else:
247 node = Node(value_repr=to_repr(obj), last=root)
248 node.is_tuple = _safe_isinstance(obj, tuple)
249 node.is_namedtuple = _is_namedtuple(obj)
250 return node